﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using NetOffice;
using Word = NetOffice.WordApi;
using NetOffice.WordApi.Enums;
using Office = NetOffice.OfficeApi;
using NetOffice.OfficeApi.Enums;
using VBIDE = NetOffice.VBIDEApi;
using NetOffice.VBIDEApi.Enums;
using NetOffice.Tools;
using NetOffice.WordApi.Tools;

namespace WordCrossRefAddin {

    // sec: Addin core

    [COMAddin("WordCrossRefAddin", "Cross Reference Insertion Helper", 3), CustomUI("WordCrossRefAddin.RibbonUI.xml")]
    [GuidAttribute("1F68E910-AA62-458D-B28F-F8D4A1D06468"), ProgId("WordCrossRefAddin.Addin")]
    public class Addin : NetOffice.WordApi.Tools.COMAddin {

        public static SimonPG.Office.WordAddinHelper<Addin, Config, Office._CustomTaskPane> Core;
        public static CrossRefForm CrossRefForm = null;

        // sec: init application

        public Addin() {

            Addin.Core = new SimonPG.Office.WordAddinHelper<Addin, Config, Office._CustomTaskPane>(
                this,
                () => this.Application,
                () => Config.Load());
            Addin.Core.OnNeedToCreateCtpByDoc += _WordAddinHelper_OnNeedToCreateCtpByDoc;
            Addin.Core.OnNeedToSaveCtpWhenEnd += _WordAddinHelper_OnNeedToSaveCtpWhenEnd;
            
            Config.FileName = "wcr_addin_config.json";
        }

        // sec: CustomTaskPane

        void _WordAddinHelper_OnNeedToCreateCtpByDoc(Word.Window window, SimonPG.Office.InfoByDocManager<Office._CustomTaskPane>.Set set) {

            // sec: init CrossRefForm

            if (Addin.CrossRefForm == null) { // if: 最初の1回目

                var form = new CrossRefForm();
                form.WordApp = Application;
                form.WordWindow = Application.ActiveWindow;
                Addin.CrossRefForm = form;
            }

            // sec: init CustomTaskPane & recover config

            var ctp = this.TaskPaneFactory.CreateCTP(typeof(CrossRefPane).FullName, "相互参照リスト");
            var ctp_c = Addin.Core.Config.Ctp;
            ctp.DockPosition = ctp_c.DockPosition;
            ctp.DockPositionRestrict = MsoCTPDockPositionRestrict.msoCTPDockPositionRestrictNone;
            try {
                ctp.Height = ctp_c.Height;
            } catch { }
            try {
                ctp.Width = ctp_c.Width;
            } catch { }
            ctp.Visible = ctp_c.Visible;
            set.Info = ctp;

            // sec: init UserControl & recover config

            var pane = ctp.ContentControl as CrossRefPane;
        }

        void _WordAddinHelper_OnNeedToSaveCtpWhenEnd(Word.Window window, SimonPG.Office.InfoByDocManager<Office._CustomTaskPane>.Set set) {

            Addin.CrossRefForm.Close();

            // sec: save UserControl

            var ctp = set.Info;
            var pane = ctp.ContentControl as CrossRefPane;
            pane.Dispose();

            // sec: save CustomTaskPane

            var ctp_c = Addin.Core.Config.Ctp;
            ctp_c.DockPosition = ctp.DockPosition;
            ctp_c.Height = ctp.Height;
            ctp_c.Width = ctp.Width;
            ctp_c.Visible = ctp.Visible;
            ctp.Dispose();

            Addin.Core.Config.Save();
        }

        // sec: Ribbon UI
        // ref: http://msdn.microsoft.com/ja-jp/library/aa722523(v=office.12).aspx#a16c7df5-93f3-4920-baa8-7b7290794c15_Ribbon

        #region IRibbonExtensibility Members

        public void ShowPane_OnAction(Office.IRibbonControl control, bool pressed) {
            var ctp = Addin.Core.InfoByDoc.GetInfo(Application.ActiveWindow);
            ctp.Visible = pressed;
        }
        public bool ShowPane_GetPressed(Office.IRibbonControl control) {
            var ctp = Addin.Core.InfoByDoc.GetInfo(Application.ActiveWindow);
            return ctp.Visible;
        }

        public void ShowForm_OnAction(Office.IRibbonControl control, bool pressed) {
            Addin.CrossRefForm.Show();
        }
        public bool ShowForm_GetPressed(Office.IRibbonControl control) {
            return Addin.CrossRefForm.Visible;
        }

        public void ShowConfigFile_OnAction(Office.IRibbonControl control) {
            var path = System.IO.Path.Combine(Config.FilePath, Config.FileName);
            System.Diagnostics.Process.Start(path);
        }

        #endregion
    }

    public class Debug {
        public static void WriteLine(string format, params object[] args) {
            System.IO.File.AppendAllText(
                System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)), "append.log"), 
                DateTime.Now + ": " + string.Format(format, args) + "\r\n");
        }
    }
}
